
//继承与多态
//class Dog{
//    public String name;
//    public int age;
//    public String color;
//
//    public void eat(){
//        System.out.println(this.name + "正在吃饭");
//    }
//    public void barks(){
//        System.out.println(this.name + "汪汪叫~");
//    }
//    public void wag(){
//        System.out.println(this.name + "摇尾巴");
//    }
//}
//class Cat{
//    public String name;
//    public int age;
//    public String color;
//
//    public void eat(){
//        System.out.println(this.name + "正在吃饭");
//    }
//    public void miao(){
//        System.out.println(this.name + "喵喵~");
//    }
//    public void behavior(){
//        System.out.println(this.name + "会爬树");
//    }
//
//}

//父类
class Animal{
        public String name;
        public int age;
        public String color;

        //父类Animal静态代码块
        static{
            System.out.println("Animal的静态代码块！");
        }

        //父类实例代码块
        {
            System.out.println("Animal的实例代码块！");
        }

        public Animal(String name){
            this.name  = name;
            System.out.println("Animal(String)");
        }
        public void eat() {
            System.out.println(this.name + "正在吃饭");
        }
}


//子类继承父类
//子类将父类中的成员变量或成员方法继承到子类之中
//子类继承父类之后，需要添加新的特有成员，显现出与父类的不同，否则就没有必要继承
class Dog extends Animal{

    //父类Animal静态代码块
    static{
        System.out.println("Dog的静态代码块！");
    }

    //父类实例代码块
    {
        System.out.println("Dog的实例代码块！");
    }

    //子类的构造方法
    public Dog(String name){
        //调用基类的构造方法
        super(name);//调用基类构造时，必须为子类构造函数中的第一天语句
        System.out.println("Dog(String)");
    }
    public void barks(){
        System.out.println(this.name + "汪汪叫~");
    }
    public void wag(){
        System.out.println(this.name + "摇尾巴");
    }

    public void eat() {
        System.out.println(this.name + "正在吃狗粮！");
    }

}
class Cat extends Animal{


    public Cat(String name) {
        super(name);
    }

    public void miao(){
        System.out.println(this.name + "喵喵~");
    }
    public void behavior(){
        System.out.println(this.name + "会爬树");
    }

}

class Base{
    public int a = 1;
    public int b = 2;

    public int c = 3;
    public void func1(){
        System.out.println("Base中的func1()");
    }
}
class Derived extends Base{

    public int a = 4;
    public int d = 5;

    public void func1(){
        System.out.println("Derived中的func1()");
    }
    public void func2(){
        System.out.println("Derived中的func2()");
    }
    public void method(){

        //用this访问，不仅可以访问父类，也可以访问子类
        //当this访问父类和子类同名的成员变量时，优先访问子类
        //super只能访问从父类继承过来的成员变量

        System.out.println(this.a);
        System.out.println(this.b);
        System.out.println(this.c);
        System.out.println(this.d);
        System.out.println("==============");
        System.out.println(super.a);
        System.out.println(super.b);
        System.out.println(super.c);
        System.out.println("==============");

        //System.out.println(super.d);//error

        func1();
        super.func1();

        func2();
    }
}
public class Main {

    public static void main(String[] args){
        //向下转型
        //不安全
        //使用前需要进行检查
        Animal animal1 = new Dog("小黑");
        if(animal1 instanceof Dog){
            Dog d = (Dog)animal1;
            d.barks();
        }
        else{
            System.out.println("转型错误");
        }

//        Animal animal2 = new Dog("小白");
//        Cat c = (Cat)animal2; //抛异常 .ClassCastException
//        c.miao();

    }
    public static void main4(String[] args){
        //向上转型(从小范围向大范围转化)
        //有三种方式可以发生向上转型
        //1.直接赋值
        //2.方法传参
        //3.方法返回值

        //父类对象引用了子类对象
        Animal dog = new Dog("小黑");
        dog.age = 18;
        dog.eat();//通过父类引用调用了子类的eat方法，称之为运行时绑定(动态绑定)
        func1(dog);

        func1(new Cat("小花"));

        Animal a = func2();
        System.out.println(a.name);

    }
    public static void func1(Animal animal){
        System.out.println(animal.name);
    }

    public static Animal func2(){
        return new Dog("旺财");
    }
    public static void main3(String[] args){
        Dog dog1 = new Dog("小黑");
        System.out.println("======================");
        Dog dog2 = new Dog("小黄");


    }

    public static void main2(String[] args){
        Derived d = new Derived();
        d.method();
    }
    public static void main1(String[] args) {
        Dog d = new Dog("旺财");
        d.name = "旺财";
        d.eat();
        d.wag();
        System.out.println("======================");
        Cat c = new Cat("小花");
        c.name = "小花";
        c.eat();
        c.behavior();
    }
}